//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.dstz.bpm.engine.action.handler.instance;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.bpm.act.service.ActInstanceService;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.constant.EventType;
import com.dstz.bpm.api.constant.InstanceStatus;
import com.dstz.bpm.api.constant.NodeType;
import com.dstz.bpm.api.constant.OpinionStatus;
import com.dstz.bpm.api.engine.action.cmd.InstanceActionCmd;
import com.dstz.bpm.api.engine.context.BpmContext;
import com.dstz.bpm.api.engine.plugin.cmd.ExecutionCommand;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.model.inst.IBpmInstance;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.core.manager.BpmTaskManager;
import com.dstz.bpm.core.manager.BpmTaskOpinionManager;
import com.dstz.bpm.core.manager.BpmTaskStackManager;
import com.dstz.bpm.core.model.BpmInstance;
import com.dstz.bpm.core.model.BpmTask;
import com.dstz.bpm.core.model.BpmTaskOpinion;
import com.dstz.bpm.engine.action.cmd.DefaultInstanceActionCmd;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import com.dstz.bpm.engine.action.handler.AbsActionHandler;
import com.dstz.org.api.model.IUser;
import com.dstz.sys.util.ContextUtil;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class InstanceManualEndActionHandler extends AbsActionHandler<DefualtTaskActionCmd> {
   @Autowired
   BpmTaskOpinionManager j;
   @Autowired
   ActInstanceService i;
   @Autowired
   BpmTaskManager h;
   @Autowired
   ExecutionCommand aF;
   @Autowired
   BpmTaskStackManager k;

   public InstanceManualEndActionHandler() {
   }

   @Override
   @Transactional
   public void execute(DefualtTaskActionCmd model) {
      BpmTask task = (BpmTask)this.h.get(model.getTaskId());
      if (task == null) {
         throw new BusinessException(BpmStatusCode.TASK_NOT_FOUND);
      } else {
         model.setBpmTask(task);
         model.setDefId(task.getDefId());
         model.setBpmInstance((IBpmInstance)this.f.get(model.getInstanceId()));
         this.l(model);
         BpmContext.setActionModel(model);
         this.b(model);
         BpmInstance topInstance = this.f.getTopInstance((BpmInstance)model.getBpmInstance());
         if (topInstance != null) {
            model.setBpmInstance(topInstance);
            model.setDefId(topInstance.getDefId());
         }

         this.c(model);
         BpmContext.removeActionModel();
      }
   }

   public ActionType getActionType() {
      return ActionType.MANUALEND;
   }

   public int getSn() {
      return 6;
   }

   public Boolean isSupport(BpmNodeDef nodeDef) {
      NodeType nodeType = nodeDef.getType();
      return nodeType != NodeType.USERTASK && nodeType != NodeType.SIGNTASK ? false : true;
   }

   public Boolean isDefault() {
      return false;
   }

   public String getConfigPage() {
      return "/bpm/task/taskOpinionDialog.html";
   }

   public String getDefaultGroovyScript() {
      return "";
   }

   public String getDefaultBeforeScript() {
      return "";
   }

   public void b(DefualtTaskActionCmd actionModel) {
      BpmTaskOpinion bpmTaskOpinion = this.j.getByTaskId(actionModel.getTaskId());
      if (bpmTaskOpinion != null) {
         bpmTaskOpinion.setStatus(OpinionStatus.getByActionName(actionModel.getActionName()).getKey());
         bpmTaskOpinion.setApproveTime(new Date());
         bpmTaskOpinion.setDurMs(bpmTaskOpinion.getApproveTime().getTime() - bpmTaskOpinion.getCreateTime().getTime());
         bpmTaskOpinion.setOpinion(actionModel.getOpinion());
         IUser user = ContextUtil.getCurrentUser();
         if (user != null) {
            bpmTaskOpinion.setApprover(user.getUserId());
            bpmTaskOpinion.setApproverName(user.getFullname());
         }

         this.j.update(bpmTaskOpinion);
      }
   }

   public void c(DefualtTaskActionCmd model) {
      BpmInstance instance = (BpmInstance)model.getBpmInstance();
      if (instance == null) {
         instance = (BpmInstance)this.f.get(model.getInstanceId());
         model.setBpmInstance(instance);
      }

      if (this.i.getProcessInstance(instance.getActInstId()) != null) {
         this.i.deleteProcessInstance(instance.getActInstId(), model.getOpinion());
      }

      this.h.removeByInstId(instance.getId());
      this.p.removeByInstId(instance.getId());
      instance.setStatus(InstanceStatus.STATUS_MANUAL_END.getKey());
      instance.setEndTime(new Date());
      instance.setDuration(instance.getEndTime().getTime() - instance.getCreateTime().getTime());
      this.f.update(instance);
      this.k.removeByInstanceId(model.getInstanceId());
      this.aF.execute(EventType.MANUAL_END, this.myd(model));
      List<BpmInstance> subs = this.f.getByPId(instance.getId());
      Iterator var4 = subs.iterator();

      while(var4.hasNext()) {
         BpmInstance inst = (BpmInstance)var4.next();
         model.setBpmInstance(inst);
         model.setDefId(inst.getDefId());
         this.c(model);
      }

   }

   private InstanceActionCmd myd(DefualtTaskActionCmd model) {
      DefaultInstanceActionCmd instanceActionCmd = new DefaultInstanceActionCmd();
      instanceActionCmd.setActionName(model.getActionName());
      instanceActionCmd.setActionVariables(model.getActionVariables());
      instanceActionCmd.setBizDataMap(model.getBizDataMap());
      instanceActionCmd.setBpmDefinition(model.getBpmDefinition());
      instanceActionCmd.setBpmIdentities(model.getBpmIdentities());
      instanceActionCmd.setBpmInstance(model.getBpmInstance());
      instanceActionCmd.setBusData(model.getBusData());
      instanceActionCmd.setBusinessKey(model.getBusinessKey());
      instanceActionCmd.setDataMode(model.getDataMode());
      instanceActionCmd.setDefId(model.getDefId());
      instanceActionCmd.setDestination(model.getDestination());
      instanceActionCmd.setFormId(model.getFormId());
      return instanceActionCmd;
   }

   protected void e(DefualtTaskActionCmd model) {
   }

   protected void f(DefualtTaskActionCmd data) {
   }

   public void g(DefualtTaskActionCmd actionModel) {
   }

   protected void h(DefualtTaskActionCmd actionModel) {
   }

   @Override
   protected void i(DefualtTaskActionCmd actionModel) {

   }

}
